package cn.good.yan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


/**
 * Security配置类
 *
 * @author shengren.yan
 * @create 2024/9/15
 */
@Configuration
@EnableWebSecurity
public class Security1Config extends WebSecurityConfigurerAdapter {


    // 定义密码加密规则
    @Bean
    public PasswordEncoder getPw() {
        return new BCryptPasswordEncoder();
    }

    // 配置密码模式
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 暂时关闭csrf攻击 - csrf是跨站请求伪造
        http.csrf(csrf -> csrf.disable());
        http.authorizeRequests()
                // 允许这些方法的请求
                .antMatchers("/oauth/**", "/login/**", "/logout/**").permitAll()
                // 所有请求都必须认证，登录才能访问
                .anyRequest().authenticated()
                // 可以使用 and 来区分
                .and()
                // 允许表单的提交
                .formLogin().permitAll();

    }




}
